package com.compuvest.tld;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import java.util.Map;
import java.util.Enumeration;

import com.compuvest.web.Items;

public class Navigation extends TagSupport {

    private int categoryID;
    private int subcategoryID;
    private int lowPrice;
    private int hiPrice;
    private Items items;
    private String popUrl;
    private Map<Integer,String> popData;

//----------------------------
//
    @Override
    public int doStartTag() throws JspException {

//        long startEvent = System.currentTimeMillis();
        String pageName = (String)pageContext.getSession().getAttribute("pageName");
        String thisURL = (String)pageContext.getSession().getAttribute("thisURL");
        String serverName = pageContext.getServletContext().getInitParameter("http");
        if(thisURL == null)
            thisURL = serverName;

        StringBuilder line = new StringBuilder(1000);
        line.append("<div id='cv_nav'>");

        line.append("<a href='").append(serverName).append("'>Home</a> &#8226; ");
        if(pageName != null) {
            line.append("<a href='").append(thisURL).append("'>");
            line.append(pageName).append("</a> &#8226; ");
        }

        if(categoryID != 0) {
            line.append("<a href='").append(thisURL);
            line.append("?pg=0&cid=").append(categoryID).append("'>");
            line.append(items.getCategoryName()).append("</a> &#8226; ");
            if(subcategoryID != 0) {
                line.append("<a href='").append(thisURL).append("?pg=0&cid=").append(categoryID);
                line.append("&sid=").append(subcategoryID).append("'>");
                line.append(items.getSubcategoryName()).append("</a> &#8226; ");
            }
        }
        if(lowPrice != 0 && hiPrice != 0) {
            line.append("<a href='#'>").append(String.format("$%,.2f - $%,.2f", (float)lowPrice, (float)hiPrice)).append("</a> ");

            line.append("<a href='").append(thisURL).append("?pg=0&cid=").append(categoryID);
            line.append("&sid=").append(subcategoryID).append("&lop=0&hip=0").append(popUrl);
            line.append("' title='Remove Price Range'>").append("<nobr>(x)</nobr>").append("</a> &#8226; ");
        }
        if(popData != null) {
            for(int key : popData.keySet()) {

                line.append("<a href='#'>").append(popData.get(key)).append("</a> ");

                line.append("<a href='").append(thisURL).append("?pg=0&cid=").append(categoryID);
                line.append("&sid=").append(subcategoryID).append("&lop=").append(lowPrice).append("&hip=").append(hiPrice);
                line.append("&p").append(key).append(popUrl);
                line.append("' title='Remove ").append(popData.get(key)).append("'>");
                line.append("<nobr>(x)</nobr>").append("</a> &#8226; ");
            }
        }
        line.append(items.getEventTime());
        line.append("</div>\n");

        try {
            pageContext.getOut().print(line.toString());
//            pageContext.getOut().print(line.toString() +"<br>"+line.length()+" t="+(int)(System.currentTimeMillis()-startEvent));
        }
        catch(java.io.IOException e) {
                throw new JspTagException("Navigation: " + e.getMessage());
        }

        return SKIP_BODY;
    }

//----------------------------
//
    @Override
    public int doEndTag() {
        return EVAL_PAGE;
    }

//----------------------------
//
    @Override
    public void release() {
        items = null;
        popUrl = null;
        popData = null;
    }

//----------------------------
//
    public void setCategoryID(int categoryID)               { this.categoryID = categoryID; }
    public void setSubcategoryID(int subcategoryID)         { this.subcategoryID = subcategoryID; }
    public void setLowPrice(int lowPrice)                   { this.lowPrice = lowPrice; }
    public void setHiPrice(int hiPrice)                     { this.hiPrice = hiPrice; }
    public void setItems(Items items)                       { this.items = items; }
    public void setPopUrl(String popUrl)                    { this.popUrl = popUrl; }
    public void setPopData(Map<Integer,String> popData)     { this.popData = popData; }
}
